'use strict';

//引用nunjucks模块
let nunjucks = require('nunjucks');

//创建nunjucks的environment实例
function createEnv(path,opts){
    path = path || 'views';
    opts = opts || {};
    let envOptions ={
    autoescape:opts.autoescape === undefined?false:opts.autoescape,
    throwOnUndefined:opts.throwOnUndefined === undefined?false:opts.throwOnUndefined,
    watch:opts.watch === undefined?false:opts.watch,
    noCache:opts.noCache === undefined?false:opts.noCache     
    }
   let env = nunjucks.configure(path,envOptions);
   return env;
}

//暴露出去，为函数
module.exports = async (ctx,next)=>{
    
    ctx.render = function(views,modle){
    //调用函数    
     let cre = createEnv();
     //渲染数据响应到服务器
    ctx.response.body = cre.render(views,modle);   
    }
    //读取下一个异步函数
    await next();
}